let app = getApp();

Page({
  data: {
    currentContentId: 0,   //默认展示第一个未完成的内容
    purchaseOrderList: [],
    searchInput:"",

  },
  onLoad() {
    this.getPurchaseOrderList('');   
  },

  // 点击了哪个状态
  toApprovalHall(e) {
    const contentId = e.currentTarget.dataset.id; // 获取点击的内容的id
    console.log("我点击了第几个=====>",contentId);
    var that = this;
    that.setData({
      currentContentId: contentId, // 更新当前展示的内容id
      page:1,
      purchaseOrderList:[],
      dowm:true,
    });

    if(contentId < 1){
      that.getPurchaseOrderList('');
    }else{
      that.getPurchaseOrderListEnd('');
    }

  },

    // 扫码
    onSearchShop() {
      const that = this;
      dd.scan({
          type: 'qr',
          success: (res) => {
              console.log("扫码结果", res.code);
              that.onSearchInput({ detail: { value: res.code } }); // 调用输入框值改变事件，并传入扫码结果
          },
          fail: (err) => {
              // 扫码失败后的回调函数，可以根据实际需求进行处理
          }
      });
  },

  // 获取输入内容进行搜索
  onSearchInput(e){
    console.log("e===>>",e)
    var that = this;
    that.setData({
      searchInput:e,
      page:1,
      dowm:true,
      purchaseOrderList:[]
    })
    
    that.getPurchaseOrderList();

  },

// 用于监听输入框值改变事件
onSearchInput(e) {
  var that = this;
  var searchTerm = e.detail.value.trim(); // 获取输入框的搜索内容并去除首尾空格
  console.log("输入框用来搜索的内容", searchTerm);

  // 清除之前的搜索定时器
  clearTimeout(that.data.searchTimer);

  // 设置一个新的定时器，在用户停止输入300毫秒后执行搜索操作
  var searchTimer = setTimeout(function() {
      that.setData({
          searchTimer: null // 清空定时器
      });
      that.getPurchaseOrderList(searchTerm); // 执行搜索操作
      that.getPurchaseOrderListEnd(searchTerm); // 执行搜索操作
  }, 300);

  // 将定时器存储在数据中
  that.setData({
      searchTimer: searchTimer
  });
},


// 清除搜索框内容
onSearchClear(e) {
  var that = this;
  that.setData({
    searchInput: '', // 清空搜索框内容
    page: 1,
    dowm: true,
    purchaseOrderList: []
  });
},

  // 获取未完成数据
  getPurchaseOrderList(searchTerm) {
    console.log("进入了");
    var that = this;

    // 在请求开始前先清空数组
    that.setData({
      purchaseOrderList: []  // 清空数组
    });

    var shujuKu = app.globaldata.cipher_code
    console.log("数据库==>",shujuKu);
   
    var data = JSON.stringify({
      "q": {
        "encrypted_text": shujuKu,
        "key": "66249c7f42e1e400",
        "sql": "select t1.fid as fid,t1.F_ZYKJ_Date_1xj as 装箱日期,t1.FBillNo as 单据编号,t1.F_ZYKJ_Text_k79 AS 订单号,t1.F_ZYKJ_Text_ca9 as 箱号,t1.F_ZYKJ_Qty_dvn as 体积,t1.F_ZYKJ_Qty_w5c as 重量,t1.F_ZYKJ_Combo_yrr as 箱规,T4.FNAME AS 装箱员,SUM(T2.F_ZYKJ_BaseQty_pzg) as 基本总数量,sum(T2.F_ZYKJ_Qty_795) AS 总辅助数量 from ZYKJ_ZXD t1 inner join ZYKJ_ZXD_Entry t2 on t2.fid=t1.fid left JOIN T_BD_MATERIAL_L T3 ON T3.FMATERIALID=T2.F_ZYKJ_Base_3iy LEFT JOIN V_BD_OPERATORSTAFF_L T4 ON T4.FID=T2.F_ZYKJ_Base_qtr WHERE t1.F_ZYKJ_Text_k79 LIKE '%" + searchTerm + "%' and t1.FDocumentStatus <> 'C' group by t1.fid,t1.F_ZYKJ_Date_1xj,t1.FBillNo,t1.F_ZYKJ_Text_k79,t1.F_ZYKJ_Text_ca9,t1.F_ZYKJ_Qty_dvn,t1.F_ZYKJ_Qty_w5c,t1.F_ZYKJ_Combo_yrr,T4.FNAME order by t1.fbillno",
        "fields": [
          "fid","装箱日期","单据编号","订单号","箱号","体积","重量","箱规","装箱员","基本总数量","总辅助数量"
        ]
      },
      "p": {
        "page_number": 1,
        "page_size": 20
      }
      })

      console.log("data==>",data);

    dd.httpRequest({
      url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: data,
      dataType: 'json',
      success: function (res) {
        console.log('获取配货单未完成数据响应==>', res.data)
        if (res.data.code == 200) {
        console.log('获取配货单未完成数据响应200000==>', res.data)
          var list = res.data.data;
          var purchaseOrderList = that.data.purchaseOrderList;
          for(var i in list){
            purchaseOrderList.push(list[i])
          }
          if(list.length < that.data.page_size){
            that.setData({
              dowm:false
            })
          }
          that.setData({
            purchaseOrderList:purchaseOrderList
          })
          console.log("数据",purchaseOrderList);
        } else {
          console.log("配货未完成的无数据");
          // dd.showToast({
          //   type: 'fail',
          //   content: res.data.data,
          //   duration: 2000,
          // });
        }
      },
      fail: function (res) {
        // dd.alert({ content: 'fail' });
      },
    });
  },

    // 获取已经完成数据
    getPurchaseOrderListEnd(searchTerm) {
      console.log("进入了");
      var that = this;
      that.setData({
        purchaseOrderList: []  // // 在请求开始前先清空数组 清空数组
      });

      var shujuKu = app.globaldata.cipher_code
      console.log("数据库==>",shujuKu);

      var data = JSON.stringify({
        "q": {
          "encrypted_text": shujuKu,
          "key": "66249c7f42e1e400",
          "sql": "select t1.fid as fid,t1.F_ZYKJ_Date_1xj as 装箱日期,t1.FBillNo as 单据编号,t1.F_ZYKJ_Text_k79 AS 订单号,t1.F_ZYKJ_Text_ca9 as 箱号,t1.F_ZYKJ_Qty_dvn as 体积,t1.F_ZYKJ_Qty_w5c as 重量,t1.F_ZYKJ_Combo_yrr as 箱规,T4.FNAME AS 装箱员,SUM(T2.F_ZYKJ_BaseQty_pzg) as 基本总数量,sum(T2.F_ZYKJ_Qty_795) AS 总辅助数量 from ZYKJ_ZXD t1 inner join ZYKJ_ZXD_Entry t2 on t2.fid=t1.fid left JOIN T_BD_MATERIAL_L T3 ON T3.FMATERIALID=T2.F_ZYKJ_Base_3iy LEFT JOIN V_BD_OPERATORSTAFF_L T4 ON T4.FID=T2.F_ZYKJ_Base_qtr WHERE t1.F_ZYKJ_Text_k79 LIKE '%" + searchTerm + "%' and t1.FDocumentStatus ='C' group by t1.fid,t1.F_ZYKJ_Date_1xj,t1.FBillNo,t1.F_ZYKJ_Text_k79,t1.F_ZYKJ_Text_ca9,t1.F_ZYKJ_Qty_dvn,t1.F_ZYKJ_Qty_w5c,t1.F_ZYKJ_Combo_yrr,T4.FNAME order by t1.fbillno",
          "fields": [
            "fid","装箱日期","单据编号","订单号","箱号","体积","重量","箱规","装箱员","基本总数量","总辅助数量"
          ]
        },
        "p": {
          "page_number": 1,
          "page_size": 20
        }
        })
  
      dd.httpRequest({
        url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
        method: 'POST',
        headers: {
          "Content-Type": "application/json"
        },
        data: data,
        dataType: 'json',
        success: function (res) {
          console.log('获取配货单未完成数据响应==>', res.data)
          if (res.data.code == 200) {
          console.log('获取配货单未完成数据响应200000==>', res.data)
            var list = res.data.data;
            var purchaseOrderList = that.data.purchaseOrderList;
            for(var i in list){
              purchaseOrderList.push(list[i])
            }
            if(list.length < that.data.page_size){
              that.setData({
                dowm:false
              })
            }
            that.setData({
              purchaseOrderList:purchaseOrderList
            })
            console.log("数据",purchaseOrderList);
          } else {
            console.log("配货未完成的无数据");
            // dd.showToast({
            //   type: 'fail',
            //   content: res.data.data,
            //   duration: 2000,
            // });
          }
        },
        fail: function (res) {
          // dd.alert({ content: 'fail' });
        },
      });
    },



  // 已完成点击查看详情
  gopickingListTwoy(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    console.log("已完成的item",item);
    dd.navigateTo({
      url: '/pages/boxNewListEd/boxNewListEd?item='+JSON.stringify(item),

    })
  },

  // 未完成点击查看详情
  goboxNewListIng(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    console.log("已完成的item",item);
    dd.navigateTo({
      url: '/pages/boxNewListIng/boxNewListIng?item='+JSON.stringify(item),
    })
  },

    // 新增装箱单
    goXinzeng(e) {
      var that = this
      var data = JSON.stringify(
        {
          "save": {
            "formid": "ZYKJ_ZXD",
            "model": { "FID": 0,"FBillNo": "","F_ZYKJ_Text_k79": "装箱单","FEntity": [{"FEntryID": 0,"F_ZYKJ_Base_3iy": {"FNUMBER": ""},"F_ZYKJ_BaseQty_pzg": 0,"F_ZYKJ_Base_re5": {"FNUMBER": ""},"F_ZYKJ_Base_qtr": {"FNUMBER": ""}}]},
            "IsAutoSubmitAndAudit": false,
            "IsDeleteEntry": false
          },
          "login": {
            "acctID": "66249c7f42e1e4",
            "url": "192.168.17.253",
            "username": "test",
            "password": "zz333333"
          }
        }
      )
  
      my.showLoading({
        content: '',
      })
      console.log("点击提交创建的data",data);
    
      dd.httpRequest({
        url: 'http://cloud.reece-sewing.com:9351/king_save',
        method: 'POST',
        headers: {
          "Content-Type": "application/json"
        },
        data: data,
        dataType: 'json',
        success: function (res) {
          if(res.data.code == 200){
          console.log("下推按钮的success响应200======》",res);

          var dataxiatui = res.data.data
          console.log("ttttttttttt",dataxiatui);
          console.log("idddddddddddddddd====>",dataxiatui.Result.ResponseStatus.SuccessEntitys[0].Id);
  
          dd.confirm({
            title: '温馨提示',
            content: '是否确认新增装箱单',
            confirmButtonText: '确定新增',
            cancelButtonText: '暂不需要',
            success: (result) => {
              console.log(result);
              if (result.confirm) {
                dd.navigateTo({
                  url: '/pages/boxNewList/boxNewList?id=' + dataxiatui.Result.ResponseStatus.SuccessEntitys[0].Id
                })
  
                my.hideLoading(); // 隐藏 loading 提示框
              }else {
                // 用户点击"暂不需要"的逻辑
                my.hideLoading(); // 取消 loading 提示框
              }
      
            },
          });
          // 1
  
          my.hideLoading();
          }else{
            my.hideLoading(); // 隐藏 loading 提示框
            dd.alert({ content: errorMessage });
            console.log("采购入库单修改提交按钮的code!=200响应",res);
          }
       
        },
        fail: function (res) {
          console.log("提交按钮的fail响应",res);
          my.hideLoading();
        },
        complete: function (res) {
          my.hideLoading();
        }
      });
    },

});
